package com.leyou.user.web;

import com.leyou.auth.utils.UserHolder;
import com.leyou.common.constants.RegexPatterns;
import com.leyou.pojo.dto.UserDetail;
import com.leyou.pojo.dto.UserDto;
import com.leyou.user.userService.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;

@RestController
@RequestMapping("/info")
@Validated
public class UserController {
    @Autowired
    private UserService  userService;

    /**
     * 根据用户名和密码查询用户
     * @param username
     * @param password
     * @return
     */
    @GetMapping
    public ResponseEntity<UserDto> queryUserByNameAndPsw(@RequestParam(value = "username") String username,
                                                         @RequestParam(value = "password") String password){
        UserDto userDto =userService.queryUserByNameAndPsw(username,password);
        return ResponseEntity.ok(userDto);
    }


    /**
     * 注册用户
     * @param username
     * @param password
     * @param phone
     * @param code
     * @return
     */
    @PostMapping
    //可以这样写
    // public ResponseEntity<Void>  registerUser(User user,
    //                                              @RequestParam(value = "code",required = true)String code)
    public ResponseEntity<Void>  registerUser(@RequestParam(value = "username")
                                              @Pattern(regexp = RegexPatterns.PASSWORD_REGEX,message = "用户名格式不正确")String username,
                                              @RequestParam(value = "password")
                                              @Pattern(regexp = RegexPatterns.PASSWORD_REGEX,message = "密码格式不正确")String password,
                                              @RequestParam(value = "phone")
                                              @Pattern(regexp = RegexPatterns.PHONE_REGEX,message = "电话号码格式不正确")String phone,

                                              @RequestParam(value = "code")String code){

        userService.registerUser(username,password,phone,code);
        return ResponseEntity.status(201).build();
    }

    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void>  sendSms(@RequestParam(value = "phone",required = true)String phone){
        userService.sendSms(phone);
        return ResponseEntity.noContent().build();
    }

    /**
     * 判断数据是否存在
     * @param data
     * @param type
     * @return
     */
    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean>  paramIsExist(@PathVariable(value = "data",required = true)String data,@PathVariable(value = "type",required = true)Integer type){
        Boolean exist = userService.paramIsExist(data, type);
        return ResponseEntity.ok().body(exist);
    }

    @GetMapping("/me")
    public ResponseEntity<UserDetail>  whoAmI(){
        UserDetail userDetail = UserHolder.get();
        return ResponseEntity.ok(userDetail);
    }
}
